<?php
namespace AuctionCore\Other;

use AuctionCore\Model\AuCarOwner;
use AuctionCore\AntNest\UserCenter;

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liugohua@clcw.com.cn  Blog:http://blog.sina.com.cn/saverneo        |
// +------------------------------------------------------------------------+
//


/**
 * 车主列表
 * @author       Eric Lee
 */
class CouponList extends \AuctionCore\System\ListBase
{
    /**
     * 获取列表结果
     * @param    int $page_number 每页条数
     * @param    int $page 页码
     * @return   Array
     */
    public function get($size, $page)
    {
        $m = new AuCarOwner();
        $m->where($this->condition);
        $m->field($this->fields);
        $m->order($this->order_by);
        //dump($m,true,null,false);
        if ($size < 1) $size = 1;
        if ($page < 2) $page = 1;
        $offset = ($page - 1) * $size;
        $m->limit($offset, $size);
        $ls = $m->select();
        return $ls;
    }

    public function count()
    {

    }

    /**
     * 获取代金券列表
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     * @return mixed:array(list,total)
     */
    public function getList($page = 1, $size = 10, $paramer)
    {
        $map = array();
        //类型
        if ($paramer['type']) {
            $map['c.coupon_type_id'] = array('eq', $paramer['type']);
        }
        //使用状态
        if ($paramer['use_status']) {
            switch ($paramer['use_status']) {
                case '1'://未使用
                    $map['dc.use_status'] = array('eq', '0');
                    $map['c.end_date'] = array('egt', date('Y-m-d'));
                    break;
                case '2'://已使用
                    $map['dc.use_status'] = array('eq', '1');
                    break;
                case '3'://已过期
                    $map['dc.use_status'] = array('neq', '1');
                    $map['c.end_date'] = array('lt', date('Y-m-d'));
                    break;
            }
        }
        //发放状态
        if ($paramer['status']) {
            switch ($paramer['status']) {
                case '1'://待发放
                    $map['c.assign_status'] = array('eq', '0');
                    $map['c.end_date'] = array('egt', date('Y-m-d'));
                    break;
                case '2'://已发放
                    $map['c.assign_status'] = array('eq', '1');
                    break;
                case '3' ://已过期
                    $map['c.assign_status'] = array('neq', '1');
                    $map['c.end_date'] = array('lt', date('Y-m-d'));
                    break;
            }
        }
        //关键字
        if ($paramer['keyword']) {
            $where = array();
            $where['c.coupon_no'] = array('eq', $paramer['keyword']);
            $where['u.user_name'] = array('like', '%' . $paramer['keyword'] . '%');
            $where['_logic'] = 'OR';
            $map['_complex'] = $where;
        }

        //所属城市
        if (!empty($paramer['branch_id'])) {
            $map['c.branch_company'] = array('eq', $paramer['branch_id']);
        }
        if ($paramer['grant_type']) {
             $map['c.grant_type'] = array('eq', $paramer['grant_type']);
        }

        $prefix = C('DB_PREFIX');
        $cols = 'c.grant_type,c.coupon_no,c.coupon_id,cd.dealer_id,cd.contact_person,cd.fullname,ct.type_name,c.amount,c.assign_status,dc.use_status,c.start_date,c.end_date,c.branch_company';
        $total = M('coupon')->alias('c')
            ->join("LEFT JOIN " . $prefix . "coupon_type ct ON c.coupon_type_id=ct.coupon_type_id")
            ->join("LEFT JOIN " . $prefix . "dealer_coupon dc ON dc.coupon_id=c.coupon_id")
            ->join("LEFT JOIN " . $prefix . "car_dealer cd ON cd.dealer_id=dc.dealer_id")
            ->where($map)->count();

        $list = M('coupon')->alias('c')->field($cols)
            ->join("LEFT JOIN " . $prefix . "coupon_type ct ON c.coupon_type_id=ct.coupon_type_id")
            ->join("LEFT JOIN " . $prefix . "dealer_coupon dc ON dc.coupon_id=c.coupon_id")
            ->join("LEFT JOIN " . $prefix . "car_dealer cd ON cd.dealer_id=dc.dealer_id")
            ->where($map)->limit($size)->page($page)->order("c.coupon_id desc")->select();
            // echo M()->getlastsql();

        foreach ($list as $key => $val) {
            $list[$key]['start_date'] = date('Y.m.d', strtotime($val['start_date']));
            $list[$key]['end_date'] = date('Y.m.d', strtotime($val['end_date']));
            if($val['branch_company']>0){
                $d = \AuctionCore\Account\Back\Department::getDepartById($val['branch_company']);
                $list[$key]['branch_company'] = $d['name'];
            }

            if ($val['grant_type'] == 1) {
                $list[$key]['grant_type'] = '手动发放';
            }elseif ($val['grant_type'] == 2) {
                $list[$key]['grant_type'] ='自动发放';
                // $type_name = $this->get_coupon_type($val['coupon_id']);
                // $list[$key]['type_name'] = "(活动)".$type_name;
            }
        }


        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    // private function get_coupon_type($coupon_id){
    //     $prefix = C('DB_PREFIX');
    //     $map['lc.coupon_id'] = array('eq',$coupon_id);
    //     $type_name = M('lottery_coupon')->alias('lc')->field('a.a_name')
    //             ->join("LEFT JOIN " . $prefix . "scratch_card sc ON lc.card_id = sc.cid")
    //             ->join("LEFT JOIN " . $prefix . "activity a ON a.aid = sc.a_id")
    //             ->where($map)->find();
    //     return $type_name['a_name'];
    // }

    /**
     * 获取车商在订单中使用代金券的总额
     * @param unknown $dealer_id
     * @param unknown $order_id
     * @return \Think\mixed
     * @author an
     */
    public function getSumCouponCost($dealer_id, $order_id)
    {
        $Coupon = M("coupon_log");
        $map = array();
        $map['dealer_id'] = array('eq',$dealer_id);
        $map['order_id'] = array('eq',$order_id);
        $map['occur_type'] = array('eq',1);
        return $Coupon->where($map)->sum('occur_money');
    }

    /**
     * 获取未使用的代金券总额
     * @param unknown $dealer_id
     * @param unknown $order_id
     * @return \Think\mixed
     * @author an
     */
    public function getSumCouponAvailable($dealer_id)
    {
        $map = array();
        $map['dc.dealer_id'] = array('eq',$dealer_id);
        $map['dc.use_status'] = array('eq', '0');
        $map['c.end_date'] = array('egt', date('Y-m-d'));

        $sum = M('coupon')->alias('c')->join("LEFT JOIN " . C('DB_PREFIX') . "coupon_type ct ON c.coupon_type_id=ct.coupon_type_id")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "dealer_coupon dc ON dc.coupon_id=c.coupon_id")->sum('ct.amount');

        return $sum;
    }
}